import java.util.Vector;

import sdljava.SDLException;
import sdljava.event.*;


public class CharacterSelection extends Location{
	
	private Vector<CharacterSprite> availableCharacters;
	private boolean overButton = false;

	public CharacterSelection(GameWindow gameWindow, String imageFile)throws SDLException {
		super(gameWindow, imageFile);
		availableCharacters = new Vector<CharacterSprite>();
		populateSVector();
		populateCVector();
	}
	
	public void handleEvent(SDLEvent event) throws SDLException{
		if(event.getType() == SDLEvent.SDL_MOUSEMOTION){
			if(buttons.get(0).spriteHandle(event) == true){
				availableCharacters.get(0).show(availableCharacters.get(0).getSpriteImage().get(0));
				overButton = true;
			}
			else if(buttons.get(1).spriteHandle(event) == true){
				availableCharacters.get(1).show(availableCharacters.get(1).getSpriteImage().get(0));
				overButton = true;
			}
			else if(buttons.get(2).spriteHandle(event) == true){
				availableCharacters.get(2).show(availableCharacters.get(2).getSpriteImage().get(0));
				overButton = true;
			}
			else{
				if(overButton == true){
					show();
					overButton = false;
				}
			}
		}
		if(event.getType() == SDLEvent.SDL_MOUSEBUTTONDOWN){
			for(int i = 0; i < buttons.size(); i++){
				if(buttons.get(i).spriteHandle(event) == true){
					chosenCharacter = availableCharacters.get(i);
					changeLocation = 1;
				}
			}
		}
	}
	
	private void populateCVector() throws SDLException{
		if(!availableCharacters.isEmpty()){
			availableCharacters.removeAllElements();
		}
		ImageData data = new ImageData(5,1,548,785,0,0);
		data.setZoom(0.75, 0.75);
		CharacterSprite male = new CharacterSprite(gameWindow, data, 140, 120, "bitmaps/spacesuit-red walking sprite.bmp");
		availableCharacters.add(male);
		CharacterSprite female = new CharacterSprite(gameWindow, data, 140, 120, "bitmaps/spacesuit-purple walking sprite.bmp");
		availableCharacters.add(female);
		CharacterSprite alien = new CharacterSprite(gameWindow, data, 140, 120, "bitmaps/spacesuit-green walking sprite.bmp");
		availableCharacters.add(alien);
	}
	
	private void populateSVector() throws SDLException{
		ImageData male = new ImageData(2, horizontal, 363, 113, 0, 0);
		ImageData female = new ImageData(2, horizontal, 363, 112, 0, 226);
		ImageData alien = new ImageData(2, horizontal, 363, 113, 0, 113);
		ButtonSprite tempSprite = new ButtonSprite(gameWindow, male, 850, 115,"bitmaps/character buttons.bmp");
		buttons.add(tempSprite);
		tempSprite = new ButtonSprite(gameWindow, female, 850, 340,"bitmaps/character buttons.bmp");
		buttons.add(tempSprite);
		tempSprite = new ButtonSprite(gameWindow, alien, 850, 570,"bitmaps/character buttons.bmp");
		buttons.add(tempSprite);
	}
	
	public void repopulateCVector() throws SDLException{
		populateCVector();
	}
}